#include <modules\dataModule.h>
#include <model.h>

#include <sstream>

using namespace std;

bool 
dataModule::tableExists(mysqlDatabase *db, string tablename) {
  bool found = false;

  if (db->connected()) {

    stringstream query;
    query << "SHOW TABLES" << endl;
    sql::ResultSet *rs = db->sendquery(query.str());

    while (rs && rs->next() && !found) {
      string tblname = rs->getString(1);
      found = (tblname == tablename);
    }
  }

  return found;
}

bool 
dataModule::createTable(mysqlDatabase *db, string tablename) {

  bool res = false;

  if (db->connected()) {
    stringstream query;
    query << "CREATE TABLE " << tablename << " ("
          << "id integer unsigned UNIQUE NOT NULL AUTO_INCREMENT,"
          << "x integer unsigned NOT NULL,"
          << "y integer unsigned NOT NULL,"
          << "data longblob,"
          << "PRIMARY KEY (id),"
          << "INDEX (x),"
          << "INDEX (y)"
          << ")"
          << "ENGINE=InnoDB character set utf8 collate utf8_czech_ci";
    res = db->executequery(query.str());
  }

  return res;
}

dataModule::dataModule() {

}

bool
dataModule::post(model *_model) {

  return false;
}